package com.midea.ai.air.ui.user;

import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.ui.user.fragment.LogInFragment;
import com.midea.ai.air.ui.user.fragment.RegisterFragment;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by June Kwok on 2016-3-19.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private LogInFragment LogIn_Fragment = new LogInFragment();
    private RegisterFragment Register_Fragment = new RegisterFragment();

    private TextView log_in_tv, register_tv;
//    public static Dialog myDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);
        initView();
    }

    private void initView() {
        log_in_tv = (TextView) findViewById(R.id.log_in_textView);
        register_tv = (TextView) findViewById(R.id.register_textView);

        log_in_tv.setOnClickListener(this);
        register_tv.setOnClickListener(this);

        //设置LogInFragment为初始Fragment
        startFragment(this, R.id.log_in_or_register_fragment, LogIn_Fragment);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.log_in_textView:
                //登陆界面

                log_in_tv.setTextColor(this.getResources().getColor(R.color.text_black_standard));
                register_tv.setTextColor(this.getResources().getColor(R.color.text_gray_assist));
                startFragment(this, R.id.log_in_or_register_fragment, LogIn_Fragment);
                break;
            case R.id.register_textView:
                //注册界面

                log_in_tv.setTextColor(this.getResources().getColor(R.color.text_gray_assist));
                register_tv.setTextColor(this.getResources().getColor(R.color.text_black_standard));
                startFragment(this, R.id.log_in_or_register_fragment, Register_Fragment);
                break;

            default:
                break;
        }

    }


    public static void mLog(String content) {
        String tag = "hwwlog";
        if (true) {
            Log.i(tag, content);
        }
    }

    public static void startFragment(Activity activity, int R_id_layout, Fragment fragment) {
        activity.getFragmentManager().beginTransaction().replace(R_id_layout, fragment).commit();
    }

    public static void myToast(Context context, int resId) {
        Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
    }

//    public static void showDialog(Context context, int resId) {
//        if (myDialog != null) {
//            myDialog.show();
//        } else {
//            myDialog = createLoadingDialog(context, resId);
//            myDialog.show();
//        }
//    }
//
//    public static void dismissDialog() {
//        if (myDialog != null && myDialog.isShowing()) {
//            myDialog.dismiss();
//        }
//    }

    public static Dialog createLoadingDialog(Context context, int id) {
        String msg = context.getResources().getString(id);
        mLog("myDialog_msg = " + msg);
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.myloadingdialog, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        TextView tipTextView = (TextView) v.findViewById(R.id.loadingmsg);// 提示文字
        tipTextView.setText(msg);// 设置加载信息
        Dialog loadingDialog = new Dialog(context, R.style.my_loading_dialog);// 创建自定义样式dialog
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));// 设置布局
        return loadingDialog;

    }


    public static boolean isSpecialCharacter(String string) {
        final String SC_REGEX = "[`~!@#$%^&*()+=|{}':;',\\\\\\\\[\\\\\\\\].<>/?~！@#￥%……&*（）—_+|{}【】‘；：”“’。，、？]";
        Pattern p = Pattern.compile(SC_REGEX);
        Matcher m = p.matcher(string);

        if (m.find()) {
            return true;
        } else {
            return false;
        }
    }


}
